/*
 * @Author: Wangtao
 * @Date: 2021-03-10 09:45:34
 * @LastEditors: Wangtao
 * @LastEditTime: 2022-09-11 21:32:52
 */
import M7InitWebRtc from './webrtc'
interface Options {
  server: string,
  sip: string,
  secret: string,
  onmessage?(event: string): void
  error?(error: string): void
}
interface CallConfig {
  autoAnswer: boolean
}
export default class WebPhoneSdk {
  private options: Options | undefined
  private static WebrtcObj: any
  public static CallConfig: CallConfig = {
    autoAnswer: false
  }

  public static message: ((arg0: any) => void | undefined) | undefined

  constructor (options: Options) {
    this.options = options
    WebPhoneSdk.WebrtcObj = new M7InitWebRtc(options)
    WebPhoneSdk.WebrtcObj.message = (message: any) => {
      if (WebPhoneSdk.message) {
        WebPhoneSdk.message(message)
      }
    }
  }

  public static accept () {
    if (WebPhoneSdk.WebrtcObj) {
      WebPhoneSdk.WebrtcObj.accept()
    }
  }

  public static sendDTMF (tone: string) {
    if (WebPhoneSdk.WebrtcObj) {
      WebPhoneSdk.WebrtcObj.sendDTMF(tone)
    }
  }

  public static unregister () {
    if (WebPhoneSdk.WebrtcObj) {
      WebPhoneSdk.WebrtcObj.m7UnregisterWebrtc()
    }
  }

  public static disconnect(): Promise<void> {
    return WebPhoneSdk.WebrtcObj.disconnect()
  }

  
  public static connect () {
    return WebPhoneSdk.WebrtcObj.connect()
  }

  public static isConnected(): boolean {
    return WebPhoneSdk.WebrtcObj.isConnected()
  }
}
